﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Controls;

namespace YJKArchUtils.Utils.SelectMaterial
{
	public partial class MaterialManageForm : System.Windows.Forms.Form
	{
		public MaterialManageForm(Autodesk.Revit.DB.Document doc, string oldMaterialName)
		{
			this.m_OldMaterialName = oldMaterialName;
			this.m_Doc = doc;
			this.InitializeComponent();
		}

		public ElementId SelectedMaterialId()
		{
			int index = this.listViewMaterialList.SelectedItems[0].Index;
			return this.m_MaterialList[index].Id;
		}

		private void listViewMaterialList_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.UpdateCurrentMaterialInfo();
		}

		private void MaterialManageForm_Load(object sender, EventArgs e)
		{
			this.UpdateMaterialList();
		}

		private void UpdateMaterialList()
		{
			IEnumerable<Element> enumerable = new FilteredElementCollector(this.m_Doc).OfClass(typeof(Material)).ToElements();
			this.m_MaterialList.Clear();
			this.listViewMaterialList.BeginUpdate();
			this.listViewMaterialList.Items.Clear();
			ListViewItem listViewItem = null;
			foreach (Element element in enumerable)
			{
				Material material = element as Material;
				ListViewItem listViewItem2 = new ListViewItem();
				listViewItem2.ImageIndex = 0;
				listViewItem2.Text = material.Name;
				this.listViewMaterialList.Items.Add(listViewItem2);
				this.m_MaterialList.Add(material);
				if (material.Name.CompareTo(this.m_OldMaterialName) == 0)
				{
					listViewItem = listViewItem2;
				}
			}
			if (listViewItem != null)
			{
				listViewItem.Selected = true;
			}
			this.listViewMaterialList.EndUpdate();
		}

		private void UpdateCurrentMaterialInfo()
		{
            if (this.listViewMaterialList.SelectedItems.Count != 1)
            {
                return;
            }
            int index = this.listViewMaterialList.SelectedItems[0].Index;
            Material material = this.m_MaterialList[index];
            this.btnColor.Text = string.Concat(new string[]
			{
				material.Color.Red.ToString(),
				" ",
				material.Color.Green.ToString(),
				" ",
				material.Color.Blue.ToString()
			});
            this.btnColor.BackColor = this.RColor2WColor(material.Color);
            this.btnColor.ForeColor = this.InverseColor(this.btnColor.BackColor);
            this.btnSurfacePatternColor.Text = string.Concat(new string[]
			{
				material.SurfacePatternColor.Red.ToString(),
				" ",
				material.SurfacePatternColor.Green.ToString(),
				" ",
				material.SurfacePatternColor.Blue.ToString()
			});
            this.btnSurfacePatternColor.BackColor = this.RColor2WColor(material.SurfacePatternColor);
            this.btnSurfacePatternColor.ForeColor = this.InverseColor(this.btnSurfacePatternColor.BackColor);
            FillPattern fillPattern = null;
            FillPatternElement surfacePatternWithMaterial = RevitVersionFuncs.GetSurfacePatternWithMaterial(material, this.m_Doc);
            if (surfacePatternWithMaterial != null)
            {
                fillPattern = surfacePatternWithMaterial.GetFillPattern();
            }
            this.pictureBoxSurface.Image = FillPatternDrawingKit.CreateFillPatternImage(fillPattern, (double)this.pictureBoxSurface.Width, (double)this.pictureBoxSurface.Height);
            this.btnCutPatternColor.Text = string.Concat(new string[]
			{
				material.CutPatternColor.Red.ToString(),
				" ",
				material.CutPatternColor.Green.ToString(),
				" ",
				material.CutPatternColor.Blue.ToString()
			});
            this.btnCutPatternColor.BackColor = this.RColor2WColor(material.CutPatternColor);
            this.btnCutPatternColor.ForeColor = this.InverseColor(this.btnCutPatternColor.BackColor);
            FillPattern fillPattern2 = null;
            FillPatternElement cutPatternWithMaterial = RevitVersionFuncs.GetCutPatternWithMaterial(material, this.m_Doc);
            if (cutPatternWithMaterial != null)
            {
                fillPattern2 = cutPatternWithMaterial.GetFillPattern();
            }
            this.pictureBoxCut.Image = FillPatternDrawingKit.CreateFillPatternImage(fillPattern2, (double)this.pictureBoxCut.Width, (double)this.pictureBoxCut.Height);
        }

        private Autodesk.Revit.DB.Color WColor2RColor(System.Drawing.Color wColor)
		{
            return new Autodesk.Revit.DB.Color(wColor.R, wColor.G, wColor.B);
		}

        private System.Drawing.Color RColor2WColor(Autodesk.Revit.DB.Color rColor)
		{
			return System.Drawing.Color.FromArgb((int)rColor.Red, (int)rColor.Green, (int)rColor.Blue);
		}

		private System.Drawing.Color InverseColor(System.Drawing.Color color)
		{
			if (Math.Abs((int)(color.R - 128)) < 5 && Math.Abs((int)(color.G - 128)) < 5 && Math.Abs((int)(color.B - 128)) < 5)
			{
				return System.Drawing.Color.Black;
			}
			return System.Drawing.Color.FromArgb(System.Drawing.Color.FromArgb(255, 255, 255).ToArgb() - color.ToArgb());
		}

		private void btnColor_Click(object sender, EventArgs e)
		{
			if (this.listViewMaterialList.SelectedItems.Count != 1)
			{
				return;
			}
			int index = this.listViewMaterialList.SelectedItems[0].Index;
			Material material = this.m_MaterialList[index];
			System.Drawing.Color black = System.Drawing.Color.Black;
			if (this.ShowColorDialog(this.RColor2WColor(material.Color), ref black))
			{
				try
				{
					material.Color = this.WColor2RColor(black);
					this.UpdateCurrentMaterialInfo();
				}
				catch (Exception)
				{
				}
			}
		}

		private void btnSurfacePatternColor_Click(object sender, EventArgs e)
		{
            if (this.listViewMaterialList.SelectedItems.Count != 1)
            {
                return;
            }
            int index = this.listViewMaterialList.SelectedItems[0].Index;
            Material material = this.m_MaterialList[index];
            System.Drawing.Color black = System.Drawing.Color.Black;
            if (this.ShowColorDialog(this.RColor2WColor(material.SurfacePatternColor), ref black))
            {
                try
                {
                    material.SurfacePatternColor = this.WColor2RColor(black);
                    this.UpdateCurrentMaterialInfo();
                }
                catch (Exception)
                {
                }
            }

		}

		private void btnCutPatternColor_Click(object sender, EventArgs e)
		{
            //if (this.listViewMaterialList.SelectedItems.Count != 1)
            //{
            //    return;
            //}
            //int index = this.listViewMaterialList.SelectedItems[0].Index;
            //Material material = this.m_MaterialList[index];
            //System.Drawing.Color black = System.Drawing.Color.Black;
            //if (this.ShowColorDialog(this.RColor2WColor(RevitVersionFuncs.GetCutPatternColor(material)), ref black))
            //{
            //    try
            //    {
            //        RevitVersionFuncs.SetCutPatternColor(ref material, this.WColor2RColor(black));
            //        this.UpdateCurrentMaterialInfo();
            //    }
            //    catch (Exception)
            //    {
            //    }
            //}

            if (this.listViewMaterialList.SelectedItems.Count != 1)
            {
                return;
            }
            int index = this.listViewMaterialList.SelectedItems[0].Index;
            Material material = this.m_MaterialList[index];
            System.Drawing.Color black = System.Drawing.Color.Black;
            if (this.ShowColorDialog(this.RColor2WColor(material.CutPatternColor), ref black))
            {
                try
                {
                    material.CutPatternColor = this.WColor2RColor(black);
                    this.UpdateCurrentMaterialInfo();
                }
                catch (Exception)
                {
                }
            }
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			try
			{
				Material.Create(this.m_Doc, "默认为新材质");
			}
			catch (Exception)
			{
				return;
			}
			this.UpdateMaterialList();
		}

		private void btnCopy_Click(object sender, EventArgs e)
		{
			if (this.listViewMaterialList.SelectedItems.Count != 1)
			{
				return;
			}
			int index = this.listViewMaterialList.SelectedItems[0].Index;
			Material material = this.m_MaterialList[index];
			try
			{
				material.Duplicate("默认为新材质");
			}
			catch (Exception)
			{
				return;
			}
			this.UpdateMaterialList();
		}

		private bool ShowColorDialog(System.Drawing.Color oldColor, ref System.Drawing.Color newColor)
		{
			ColorDialog colorDialog = new ColorDialog();
			colorDialog.AllowFullOpen = true;
			colorDialog.FullOpen = true;
			colorDialog.ShowHelp = false;
			colorDialog.Color = oldColor;
			if (colorDialog.ShowDialog() != DialogResult.OK)
			{
				return false;
			}
			newColor = colorDialog.Color;
			return true;
		}

		private void MaterialManageForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (base.DialogResult == DialogResult.Cancel)
			{
				return;
			}
			if (this.listViewMaterialList.SelectedItems.Count == 0)
			{
				YJKMessageBox.Show("没有选中材质类型");
				e.Cancel = true;
			}
		}

		private void pictureBoxSurface_Click(object sender, EventArgs e)
		{
			if (this.listViewMaterialList.SelectedItems.Count != 1)
			{
				return;
			}
			int index = this.listViewMaterialList.SelectedItems[0].Index;
			Material material = this.m_MaterialList[index];
			FillPattern currentFillPattern = null;
			FillPatternElement fillPatternElement = RevitVersionFuncs.GetSurfacePatternWithMaterial(material, this.m_Doc);
			if (fillPatternElement != null)
			{
				currentFillPattern = fillPatternElement.GetFillPattern();
			}
			FillPatternListForm fillPatternListForm = new FillPatternListForm(this.m_Doc, currentFillPattern);
			if (fillPatternListForm.ShowDialog() != DialogResult.OK)
			{
				return;
			}
			try
			{
				FillPattern fillPattern = fillPatternListForm.SelectedFillPattern();
				if (fillPatternElement == null)
				{
					fillPatternElement = FillPatternElement.Create(this.m_Doc, fillPattern);
					RevitVersionFuncs.SetSurfacePattern(fillPatternElement, ref material);
				}
				else
				{
					fillPatternElement.SetFillPattern(fillPattern);
				}
				this.UpdateCurrentMaterialInfo();
			}
			catch (Exception ex)
			{
				string message = ex.Message;
			}
		}

		private void pictureBoxCut_Click(object sender, EventArgs e)
		{
			if (this.listViewMaterialList.SelectedItems.Count != 1)
			{
				return;
			}
			int index = this.listViewMaterialList.SelectedItems[0].Index;
			Material material = this.m_MaterialList[index];
			FillPattern currentFillPattern = null;
			FillPatternElement fillPatternElement = RevitVersionFuncs.GetCutPatternWithMaterial(material, this.m_Doc);
			if (fillPatternElement != null)
			{
				currentFillPattern = fillPatternElement.GetFillPattern();
			}
			FillPatternListForm fillPatternListForm = new FillPatternListForm(this.m_Doc, currentFillPattern);
			if (fillPatternListForm.ShowDialog() != DialogResult.OK)
			{
				return;
			}
			try
			{
				FillPattern fillPattern = fillPatternListForm.SelectedFillPattern();
				if (fillPatternElement == null)
				{
					fillPatternElement = FillPatternElement.Create(this.m_Doc, fillPattern);
					RevitVersionFuncs.SetCutPattern(fillPatternElement, ref material);
				}
				else
				{
					fillPatternElement.SetFillPattern(fillPattern);
				}
				this.UpdateCurrentMaterialInfo();
			}
			catch (Exception ex)
			{
				string message = ex.Message;
			}
		}

		private Document m_Doc;

		private string m_OldMaterialName = "";

		private List<Material> m_MaterialList = new List<Material>();
	}
}
